// Jenkinsfile Declarative Pipeline
pipeline {
    agent {
        // 通过标签名称选择指定运行节点
        label 'father'
    }
    // 环境变量设置
    environment {
        my_pipeline_name = 'pipelineTest'
        my_deploy = 'deploy'
    }
    // 参数
    parameters{
        string(name: 'branch', defaultValue: 'master', description: 'Git branch')
    }

    stages {

        stage('stage 1') {
            steps {
                echo 'Hello World'
            }
        }

        stage('stage 2') {
            steps {
                echo 'Hello Java'
                echo 'Hello Test'
            }
        }

        stage('使用自动以环境变量') {
            steps {
                echo "environment $my_pipeline_name"
                echo "environment $my_deploy"
                echo "Git branch ${params.branch}"
//                sh ('echo my test $my_pipeline_name')
//                sh 'printenv'
                script {
                    try {
                        sh 'docker stop $my_pipeline_name'
                    }catch(ignored){
                        sh 'echo The container $my_pipeline_name dose not exist'
                    }
                }
                sh '''
                    echo $PWD
                    echo $my_pipeline_name
                    echo $my_deploy
                '''
            }
        }
    }
}